//
//  FCAssignEmployeeViewController.m
//  customerRelation_Example
//
//  Created by 陈文斌 on 2019/2/19.
//  Copyright © 2019 64912907. All rights reserved.
//

#import "FCWorkGroupEmployeesChoiceViewController.h"
#import "CustomerRelationHeader.h"
#import "FCCustomerRelationRequest.h"
#import "FCEmployeeSelectTableViewCell.h"
#import "FCEmployeeGroupView.h"
#import "FCCustomerRelationRequest.h"
#import "FCChoiceEmpScrollView.h"
#import "UIViewController+returnBlock.h"
#import "FCRelationListModel.h"
#import "FCWorkGroupListModel.h"
#import "UIImageView+WebCache.h"
#import "saveSandBox.h"


#define EmployeeCell @"FCEmployeeTableViewCell"
#define WorkGroupKey @"workGroup"
#define EmployeesKey @"employees"
#define WorkGroupCache @"workGroupCache"

@interface FCWorkGroupEmployeesChoiceViewController ()<UITableViewDelegate, UITableViewDataSource,searchDelegate>
@property (nonatomic, strong) UITableView *tableView;
// 工作组
@property (nonatomic, strong) NSMutableDictionary *workGroupDic;

// 关系数据
@property (nonatomic, strong) NSMutableArray<FCworkGroupModel *> *relationTypes;
// 表头：显示关系类型和联系人数量
@property (nonatomic, strong) NSMutableArray<FCEmployeeGroupView *> *groupArr;
// 储存被选中的员工
@property (nonatomic, strong) NSMutableArray *employeeArr;
// 滑动显示选中员工
@property (nonatomic,strong) FCChoiceEmpScrollView *employeeScrollView;

// 搜索结果
@property (nonatomic, strong) NSMutableArray *searchResults;

// 部门列表
@property (nonatomic, strong) NSMutableArray *departmentList;

@property (nonatomic, strong) NSMutableArray<FCEmployeeModel*> *workGroupEmployeeArr;

@property (nonatomic, assign) int skip;

@end

@implementation FCWorkGroupEmployeesChoiceViewController
- (NSMutableDictionary *)workGroupDic{
    if (!_workGroupDic) {
        _workGroupDic = [NSMutableDictionary dictionary];
    }
    return _workGroupDic;
}

- (NSMutableArray *)employeeArr{
    if (!_employeeArr) {
        _employeeArr = [NSMutableArray new];
    }
    return _employeeArr;
}
- (NSMutableArray<FCEmployeeGroupView *> *)groupArr{
    if (!_groupArr) {
        _groupArr = [NSMutableArray new];
    }
    return _groupArr;
}
- (NSMutableArray *)relationTypes{
    if (!_relationTypes) {
        _relationTypes = [NSMutableArray new];
    }
    return _relationTypes;
}

- (void)setupUI{
    WJCWeakSelf
    self.view.backgroundColor = FCBackColor;

    self.employeeScrollView = [[FCChoiceEmpScrollView alloc] initWithFrame:CGRectMake(0, 0, WJCScreenW - 92, 64)];
    self.employeeScrollView.backgroundColor = [UIColor whiteColor];
    
    self.employeeScrollView.empDelegate=self;
    [self.employeeScrollView refreshWithArr:nil];
    [self.view addSubview:self.employeeScrollView];
    if (![self.choicedArr isKindOfClass:[NSNull class]]&&self.choicedArr.count>0) {
        NSMutableArray *arr = [NSMutableArray arrayWithArray:self.choicedArr];
        self.employeeScrollView.Employee= arr;
    }
    self.workGroupEmployeeArr = [NSMutableArray arrayWithArray:self.choicedArr];
    self.employeeScrollView.canDelete=NO;
    
    [self.view addSubview:self.employeeScrollView];
    
    UIView *btnBGView = [UIView new];
    [self.view addSubview:btnBGView];
    btnBGView.backgroundColor = FCWhiteColor;
    [btnBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.employeeScrollView.mas_right).mas_equalTo(0);
        make.right.and.top.mas_equalTo(0);
        make.height.mas_equalTo(64);
    }];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:rightButton];
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.employeeScrollView).mas_equalTo(0);
        make.right.mas_equalTo(-16);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(34);
    }];
    [rightButton setTitle:customerRelationLocaChanged(@"global_general_complete", @"完成") forState:UIControlStateNormal];
    [rightButton setTitleColor:FCWhiteColor forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15];
    [rightButton addTarget:self action:@selector(employeeSelect:) forControlEvents:UIControlEventTouchUpInside];
    rightButton.backgroundColor = FCBlueBackColor;
    FCSETLRViewBorderRadius(rightButton, 4);
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.and.left.and.right.mas_equalTo(0);
        make.top.equalTo(weakSelf.employeeScrollView.mas_bottom).mas_equalTo(10);
    }];
    [self setupTableView];
    
    // 初始化数据 获取缓存
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getEmployeesAndDepartMent) name:employeeChanged object:nil];
    NSArray *workGroupCacheArr = [saveSandBox getDataBaseWithClass:[FCworkGroupModel class] andLimitType:@"other='FCWorkGroupEmployeesChoiceViewController'"];
    for (FCworkGroupModel *w in workGroupCacheArr) {
        NSMutableArray *employeesCacheArr = [saveSandBox getDataBaseWithClass:[FCEmployeeModel class] andLimitType:[NSString stringWithFormat:@"type='%@' and other='FCWorkGroupEmployeesChoiceViewController'",w.Id]];
        employeesCacheArr = [self filterEmployee:employeesCacheArr];
        NSDictionary *dic = @{
                              WorkGroupKey: w,
                              EmployeesKey: employeesCacheArr,
                              };
        weakSelf.workGroupDic[w.Id?:@""] = dic;
        if (employeesCacheArr.count == 0) {
            [self getWorkGroupDataWithTypeId:w.Id andPage:0 andKeyword:nil];
        }
    }
    if(weakSelf.workGroupDic.allKeys.count < 1) {
        [self getWorkGroupDataWithTypeId:nil andPage:0 andKeyword:nil];
    } else{
        if (!self.isHiddenDepartment) {
            [self getDepartment];
        }
    }
}

#pragma mark - searchDelegate
-(void)choiceEmpScrollView:(FCChoiceEmpScrollView *)empScrollView andsearchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    if (searchText.length<1) {
        self.searchResults = nil;
         [self.tableView reloadData];
    }else{
        self.searchResults = [NSMutableArray array];
        NSString *keyword=searchText;
        for (int i = 0; i < self.workGroupDic.allKeys.count; i++) {
            NSArray *employeeList = self.workGroupDic[self.workGroupDic.allKeys[i]][EmployeesKey];
            for (FCEmployeeModel *f in employeeList) {
                if(f.Name&&[f.Name rangeOfString:keyword].location !=NSNotFound){
                    [self addSearchResultsWithEmployee:f];
                } else if(f.CompanyName&&[f.CompanyName rangeOfString:keyword].location !=NSNotFound){
                    [self addSearchResultsWithEmployee:f];
                } else if(f.CompanyBrandName&&[f.CompanyBrandName rangeOfString:keyword].location !=NSNotFound){
                    [self addSearchResultsWithEmployee:f];
                } else if(f.JobTitle&&[f.JobTitle rangeOfString:keyword].location !=NSNotFound){
                    [self addSearchResultsWithEmployee:f];
                } else if(f.User.RealName&&[f.User.RealName rangeOfString:keyword].location !=NSNotFound){
                    [self addSearchResultsWithEmployee:f];
                }
            }
        }
        
        [self.tableView reloadData];
    }
}

- (void)addSearchResultsWithEmployee:(FCEmployeeModel *)employee{
    for (int i = 0; i < self.searchResults.count; i++) {
        FCEmployeeModel *oldEmployee = self.searchResults[i];
        if ([oldEmployee.Id isEqualToString:employee.Id]) {
            [self.searchResults removeObjectAtIndex:i];
        }
    }
    [self.searchResults addObject:employee];
}

- (void)setupNaviUI{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    [leftButton setTitle:customerRelationLocaChanged(@"global_general_cancel", @"取消") forState:UIControlStateNormal];
    [leftButton setBackgroundImage:customerRelationImageName(@"nav_ic_arrow_b") forState:UIControlStateNormal];
    [leftButton setTitleColor:FCTxtColor forState:UIControlStateNormal];
    leftButton.titleLabel.font = font15;
    [leftButton sizeToFit];
    [leftButton addTarget:self action:@selector(popNavigation) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftItem;
}
#pragma mark - 取消不选择
- (void)popNavigation{
    if(self.idBlock){
        self.idBlock(@{@"vc": self});
    }
}
#pragma mark - 选择完成回调
-(void)employeeSelect:(UIButton*)sender{
    if (self.idBlock) {
        self.idBlock(@{@"vc": self, @"employees": self.employeeScrollView.Employee?self.employeeScrollView.Employee:@[]});
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNaviUI];
    [self setupUI];
}

#pragma mark - 获取同事
- (void)getDepartment{
    //获取缓存数据
    NSArray *results = [FCFuChengCacheTool getHttpCacheWithUrlString:FCCompanyDepartmentDataKey parameters:nil];
    if (results.count > 0) {
        NSArray *departmentArr = [FCCompanyDepartmentModel mj_objectArrayWithKeyValuesArray:results];
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        FCworkGroupModel *workGroup = [[FCworkGroupModel alloc] init];
        workGroup.Id = @"000000";
        workGroup.name = customerRelationLocaChanged(@"global_general_colleague", @"同事");
        dic[WorkGroupKey] = workGroup;
        NSMutableArray *employeArr = [NSMutableArray array];
        
        for (FCCompanyDepartmentModel *departmentModel in departmentArr) {
            [employeArr addObjectsFromArray:departmentModel.Employees];
        }
        employeArr = [self filterEmployee:employeArr];
        dic[EmployeesKey] = employeArr;
        self.workGroupDic[workGroup.Id] = dic;
        [self.tableView reloadData];
    } else {
        [self getMyDepartmentList];
    }
}

#pragma mark - tableView
- (void)setupTableView{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = FCBackColor;
    self.tableView.separatorStyle = UITableViewCellEditingStyleNone;
    [self.tableView registerClass:[FCEmployeeSelectTableViewCell class] forCellReuseIdentifier:EmployeeCell];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.searchResults.count > 0) {
        return 1;
    }
    
    return self.workGroupDic.allKeys.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.searchResults.count > 0) {
        return 0.01f;
    }
    
    return 48;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (self.searchResults.count > 0) {
        return [UIView new];
    }
    
    FCEmployeeGroupView *groupView;
    
    if (self.groupArr.count > section) {
        groupView = self.groupArr[section];
    } else {
        groupView = [[FCEmployeeGroupView alloc] init];
        [self.groupArr addObject:groupView];
    }
    groupView.tag = section;
    [groupView addTarget:self action:@selector(changeExpandStatus:) forControlEvents:UIControlEventTouchUpInside];
    
    FCworkGroupModel *workGroup = self.workGroupDic[self.workGroupDic.allKeys[section]][WorkGroupKey];
    NSArray *employees = self.workGroupDic[self.workGroupDic.allKeys[section]][EmployeesKey];
    groupView.groupNameLabel.text = workGroup.name ?: @"";
    groupView.employeeCountLabel.text = [NSString stringWithFormat:@"%ld", employees.count];
    
    
    return groupView;
}
// 头部箭头动画
- (void)changeExpandStatus:(FCEmployeeGroupView *)sender{
    
    sender.isExpand = !sender.isExpand;
    
    if (sender.isExpand) {
        CGAffineTransform transform = CGAffineTransformMakeRotation(90 * M_PI/180.0);
        [sender.arrowImageView setTransform:transform];
        
    } else {
        CGAffineTransform transform = CGAffineTransformMakeRotation(0 * M_PI/180.0);
        [sender.arrowImageView setTransform:transform];
    }
    [self.tableView reloadData];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.searchResults.count > 0) {
        return self.searchResults.count;
    }
    
    FCEmployeeGroupView *employeeGroupView = self.groupArr[section];
    NSArray *employees = self.workGroupDic[self.workGroupDic.allKeys[section]][EmployeesKey];

    if (employeeGroupView.isExpand) {
        return employees.count;
    } else {
        return 0;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return FCScaleHeight(56);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *employees = self.workGroupDic[self.workGroupDic.allKeys[indexPath.section]][EmployeesKey];
    FCEmployeeModel *employee = employees[indexPath.row];
    
    if (self.searchResults.count > 0) {
        employee = self.searchResults[indexPath.row];
    }
    
    FCEmployeeSelectTableViewCell *cell = [[FCEmployeeSelectTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:EmployeeCell];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.isEnabled = YES;
    if (self.choicedArr) {
        for (int i = 0; i <  self.choicedArr.count ;i ++) {
            if ([self.choicedArr[i].Id isEqual:employee.Id]) {
                cell.isEnabled=NO;
            }
        }
    }
    [cell changeEnabledWithEnabled:cell.isEnabled];
    if (cell.isEnabled) {
        cell.isSelected = NO;
        if (self.employeeScrollView.Employee) {
            for (int i = 0; i <  self.employeeScrollView.Employee.count ;i ++) {
                if ([self.employeeScrollView.Employee[i].Id isEqual:employee.Id]) {
                    cell.isSelected=YES;
                }
            }
        }
        
        if (cell.isSelected) {
            cell.selectedStatusImageView.image = customerRelationImageName(@"ic_selected_circle");
        } else{
            cell.selectedStatusImageView.image = customerRelationImageName(@"ic_unselected_circle");
        }
    }
    
    
    [cell.avatarImageView sd_setImageWithURL:[NSURL URLWithString:employee.PortraitUrl] placeholderImage:headPlaceImage];
    cell.employeeNameLabel.text = employee.Name ?: @"";
    if (employee.CompanyDepartment.Name && employee.JobTitle) {
        cell.employeeDetailLabel.text = [NSString stringWithFormat:@"%@-%@",employee.CompanyDepartment.Name ?: @"",employee.JobTitle ?: @""];
    } else{
        cell.employeeDetailLabel.text = [NSString stringWithFormat:@"%@%@",employee.CompanyDepartment.Name ?: @"",employee.JobTitle ?: @""];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *employees = self.workGroupDic[self.workGroupDic.allKeys[indexPath.section]][EmployeesKey];
    FCEmployeeModel *employee = employees[indexPath.row];
    if (!self.employeeScrollView.Employee) {
        self.employeeScrollView.Employee = [NSMutableArray array];
    }
    
    if (self.maxCount == 1) {
        [self.employeeScrollView.Employee addObject:employee];
        if (self.idBlock) {
            self.idBlock(@{@"vc": self, @"employees": self.employeeScrollView.Employee?self.employeeScrollView.Employee:@[]});
        }
        return;
    }
    
    FCEmployeeSelectTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell.isEnabled) {
        return;
    }
    
    
    if (self.searchResults.count > 0) {
        employee = self.searchResults[indexPath.row];
    }
    
    if (self.employeeScrollView.Employee.count>0) {
        if (self.maxCount>0) {
            if (self.employeeScrollView.Employee.count>self.maxCount) {
                [SVProgressHUD showErrorWithStatus:customerRelationLocaChanged(@"已超过选择最大值", @"已超过选择最大值")];
                return;
            }
        }
        
        for (int i = 0; i <  self.employeeScrollView.Employee.count ;i ++) {
            if ([self.employeeScrollView.Employee[i].Id isEqual:employee.Id]) {
                [self.employeeScrollView.Employee removeObjectAtIndex:i];
                [self.employeeScrollView refreshWithArr:self.employeeScrollView.Employee];
                self.searchResults = nil;
                [tableView reloadData];
                return;
            }
        }
        [self.employeeScrollView.Employee addObject:employee];
        [self.employeeScrollView refreshWithArr:self.employeeScrollView.Employee];
        self.searchResults = nil;
        [tableView reloadData];
    }else{
        self.employeeScrollView.Employee=[NSMutableArray new];
        if (self.maxCount>0) {
            if (self.employeeScrollView.Employee.count>self.maxCount) {
                [SVProgressHUD showErrorWithStatus:customerRelationLocaChanged(@"已超过选择最大值", @"已超过选择最大值")];
                return;
            }
        }
        
        [self.employeeScrollView.Employee addObject:employee];
        [self.employeeScrollView refreshWithArr:self.employeeScrollView.Employee];
        self.searchResults = nil;
        [tableView reloadData];
    }
}

- (void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor whiteColor];
    [tableView setTableFooterView:view];
}

#pragma mark - 获取同事信息
/**
 企业部门列表
 */
- (void)getMyDepartmentList
{
    if (![FCEmployeeModel isLoginWithNotice:NO]) return;

    [FCCustomerRelationRequest getCompanyCompanydepartmentsWithCompanyId:[FCEmployeeModel getMinieCompanyId] orOtherParam:nil success:^(NSArray<FCCompanyDepartmentModel *> *result) {

        self.departmentList = result;

        [self getMyEmployeesList];
        
    } failure:^(NSError *error) {

    }];
}

/**
 企业的职员列表
 */
- (void)getMyEmployeesList
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"CompanyId"] = [FCEmployeeModel getMinieCompanyId];
    param[@"meId"] = [FCEmployeeModel getMinieEmpId];
    //    param[@"fields"] = @"Company";
    param[@"Skip"] = @(self.skip);
    param[@"Take"] = @15;
    
    [FCCustomerRelationRequest myEmployeesListWithParam:param success:^(NSMutableArray <FCEmployeeModel *> *employees) {
        
        self.skip += employees.count;
        
        for (FCCompanyDepartmentModel *department in self.departmentList) {
            if (!department.Employees.count) {
                department.Employees = [NSMutableArray array];
            }
            for (FCEmployeeModel *emp in employees) {
                if([emp.Id isEqual:[FCEmployeeModel getMinieEmpId]]){
                    continue;
                }
                if ([department.Id isEqualToString:emp.CompanyDepartmentId]) {
                    BOOL isAdd = YES;
                    for (FCEmployeeModel *oldEmp in department.Employees) {
                        if ([oldEmp.Id isEqual:emp.Id]) {
                            isAdd = NO;
                        }
                    }
                    if (isAdd) {
                        [department.Employees addObject:emp];
                    }
                }
            }
        }
        
        if (employees.count == 30) {
            [self getMyEmployeesList]; //加载下一页
            
        } else {
            //保存缓存数据
            NSMutableArray *dicArr = [FCCompanyDepartmentModel mj_keyValuesArrayWithObjectArray:self.departmentList];
            [FCFuChengCacheTool saveHttpCache:dicArr urlString:FCCompanyDepartmentDataKey parameters:nil];
            
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            FCworkGroupModel *workGroup = [[FCworkGroupModel alloc] init];
            workGroup.name = customerRelationLocaChanged(@"global_general_colleague", @"同事");
            dic[WorkGroupKey] = workGroup;
            NSMutableArray *employeArr = [NSMutableArray array];
            
            for (FCCompanyDepartmentModel *departmentModel in self.departmentList) {
                [employeArr addObjectsFromArray:departmentModel.Employees];
            }
            employeArr = [self filterEmployee:employeArr];
            dic[EmployeesKey] = employeArr;
            self.workGroupDic[@"000000"] = dic;
            [self.tableView reloadData];
        }
        
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - 获取工作组数据
-(void)getWorkGroupDataWithTypeId:(NSString *)relationTypeId andPage:(NSInteger)page andKeyword:(NSString *)keyword{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    WJCWeakSelf
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if (relationTypeId) {
        param[@"ids"] = @[relationTypeId];
    }
    if (keyword.length > 0) {
        param[@"keyword"] = keyword;
        param[@"keywordModel"] = @(2);
    }
    param[@"skip"] = @(50 * page);
    
    [FCCustomerRelationRequest getWorkGroupEmployeeWithVariables:param success:^(id resultObj) {
        FCWorkGroupListModel *workGroupList = [FCWorkGroupListModel mj_objectWithKeyValues: resultObj[@"data"][@"crmSupplyRelationTypes"]];
        for (FCworkGroupModel *workGroup in workGroupList.items) {
            
            // 每个分组底下的员工数组
            NSMutableArray *workGroupEmployees = weakSelf.workGroupDic[workGroup.Id][EmployeesKey] ?: [NSMutableArray new];
            
            for (int i = 0; i < workGroup.crmSupplyRelations.items.count; i++) {
                FCRelationModel *relation = workGroup.crmSupplyRelations.items[i];
                for (int j = 0; j < relation.relRelation.emps.count; j++) {
                    FCRelationRequestEmployeeModel *employee = relation.relRelation.emps[j];
                    [workGroupEmployees addObject:employee.employee];
                    [saveSandBox saveDataBaseClass:[FCEmployeeModel class] andObject:employee.employee orType:workGroup.Id andOther:@"FCWorkGroupEmployeesChoiceViewController"];
                }
            }
            
            // 判断分组是否请求完全部数据
            if (!(workGroup.crmSupplyRelations.pageInfo.currPage >= workGroup.crmSupplyRelations.pageInfo.totalPage)) {
                // 未完成全部请求，继续请求
                [weakSelf getWorkGroupDataWithTypeId:workGroup.Id andPage:workGroup.crmSupplyRelations.pageInfo.currPage andKeyword:keyword];
            } else {
                workGroupEmployees = [weakSelf filterEmployee:workGroupEmployees];
                NSDictionary *dic = @{
                                      WorkGroupKey: workGroup,
                                      EmployeesKey: workGroupEmployees,
                                      };
                weakSelf.workGroupDic[workGroup.Id?:@""] = dic;
                
                // 全部请求完成 添加缓存
                [saveSandBox saveDataBaseClass:[FCworkGroupModel class] andObject:workGroup orType:nil andOther:@"FCWorkGroupEmployeesChoiceViewController"];
                // 获取同事缓存
                if (!self.isHiddenDepartment) {
                    [self getDepartment];
                }
                
                [weakSelf.tableView reloadData];
            }
        }
    } failure:^(NSError *error) {
        
    }];
    
}

- (NSMutableArray <FCEmployeeModel *> *)filterEmployee:(NSMutableArray <FCEmployeeModel *> *)employees{
    NSMutableArray *arr = [NSMutableArray arrayWithArray:employees];
    if (self.filterEmployees.count > 0) {
        for (FCEmployeeModel *femp in self.filterEmployees) {
            if (employees.count > 0) {
                for (FCEmployeeModel *emp in employees) {
                    if ([femp.Id isEqual:emp.Id]) {
                        [arr removeObject:emp];
                    }
                }
            }
        }
    }
    
    return arr;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}

@end
